/*
 * Copyright (c) 2010, 2012 Luan Nguyen Thanh. All rights reserved.
 * 
 * This code is written by Luan Nguyen Thanh, with any partners involved.
 * The unauthorized use of this code should not be permitted!
 */
package MyIM.client.view;

import MyIM.client.engine.Engine;
import MyIM.client.view.component.MainMenuBar;
import MyIM.client.view.form.Dialog_Confirm;
import MyIM.client.view.lnf.MetroStyle;
import MyIM.utilities.Util;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.rmi.RemoteException;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author Luan Nguyen Thanh <tklarryonline@gmail.com>
 */
public class ClientView extends JFrame implements Observer {

    private String currentUsername, currentDisplayName;
    private Engine engine;
    private ContactsView contactView;

    public ClientView() {

        currentUsername = "tklarryonline";
        currentDisplayName = "Luan Nguyen Thanh";
        init();
    }

    public ClientView(Engine engine) {
        this.engine = engine;
        currentUsername = engine.getClient().getUsername();
        currentDisplayName = engine.getClient().getDisplayName();

        init();
        initListener();
    }

    private void init() {
        setTitle("MyIM - " + currentUsername);
//        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setIconImage(Util.makeIcon("Logo").getImage());

        initLayout();

        setMinimumSize(new Dimension(400, 600));
        setLocationRelativeTo(null);
    }

    private void initLayout() {
        setLayout(new BorderLayout(0, 5));
        setJMenuBar(new MainMenuBar(engine, currentDisplayName, null));
        setIconImage(Util.makeIcon("Logo").getImage());

        JLabel lblHeader = new JLabel("Messages");
        lblHeader.setFont(MetroStyle.FORM_TITLE);
        lblHeader.setForeground(MetroStyle.LIGHTER_MARINE);
        lblHeader.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
        add(lblHeader, BorderLayout.NORTH);
        contactView = new ContactsView(engine);
        add(contactView, BorderLayout.CENTER);
    }

    private void initListener() {
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                askOnExit();
            }
        });
    }

    private void askOnExit() {
        int result = JOptionPane.showConfirmDialog(this,
                "Closing this window will log you out and exit this app. Do it?",
                "Closing", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            try {
                engine.logout();
                setDefaultCloseOperation(EXIT_ON_CLOSE);
            } catch (RemoteException ex) {
                Logger.getLogger(ClientView.class.getName()).
                        log(Level.SEVERE, null, ex);
            }
        } else {
            setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        }
    }

    @Override
    public void update(Observable o, Object arg) {
    }

    public int createDialog(String msg, int requester, int requested, int group) {
        //return JOptionPane.showConfirmDialog(this, msg, engine.getClient().getUsername() + " Add Friend Request", JOptionPane.YES_NO_OPTION);
        new Dialog_Confirm(engine, msg, requester, requested, group);
        return -1;
    }

    public ContactsView getContactView() {
        return contactView;
    }
}
